package com.meringer.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.RowEditEvent;

import com.meringer.beans.ejb.UserAdminEJB;
import com.meringer.entities.CarModel;
import com.meringer.entities.User;

@ManagedBean(name = "userAdminBean")
@ViewScoped
public class UserAdminBean {
	@EJB
	UserAdminEJB userAdminEJB;

	private List<User> allUsers;

	public UserAdminBean() {
		allUsers = new ArrayList<User>();
	}

	@PostConstruct
	private void initCarModels() {
		System.out.println("ejbis " + userAdminEJB);
		allUsers = userAdminEJB.getUsers();
	}

	public List<User> getAllUsers() {
		return allUsers;
	}

	public void setAllUsers(List<User> allUsers) {
		this.allUsers = allUsers;
	}

	public void onEdit(RowEditEvent event) {
		User user = (User) event.getObject();
		userAdminEJB.updateUser(user);
	}

	public void onCancel(RowEditEvent event) {
		System.out.println("delete user "
				+ ((CarModel) event.getObject()).getName());
//		userAdminEJB.removeUser(((User) event.getObject()));
	}

}
